home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MoreKittiesPlease / Source / CBasicApp.cp next >
Text File  |  2000-06-23  |  7KB  |  287 lines

  1. // ===========================================================================
  2. //    CBasicApp.cp                 ©1994-1999 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //    This file contains the starter code for a basic PowerPlant project
  5.  
  6. #include "CBasicApp.h"
  7.  
  8. #include <LGrowZone.h>
  9. #include <PP_Messages.h>
  10. #include <PP_Resources.h>
  11. #include <UDrawingState.h>
  12. #include <UMemoryMgr.h>
  13. #include <URegistrar.h>
  14.  
  15. #include <LWindow.h>
  16. #include <LCaption.h>
  17. #include <LStdControl.h>
  18.  
  19. #include <DCon.h>
  20. #import <OpenTransport.h>
  21. #import <OpenTptClient.h>
  22. #import <OTSharedLibs.h>
  23. #import <UModaldialogs.h>
  24. #import <LInternetAddress.h>
  25. #import <UThread.h>
  26. #import <LThread.h>
  27. #import <cstdio>
  28. #import "OTAutoPush.h"
  29. #import "Kitties_Module.h"
  30.  
  31. #define kModuleName "MoreKittiesMod"
  32.  
  33.     // Constant declarations
  34. const ResIDT    PPob_SampleWindow            = 128;
  35.  
  36.  
  37. // ===========================================================================
  38. //    • main
  39. // ===========================================================================
  40.  
  41. int main()
  42. {                            
  43.         // Set Debugging options
  44.     SetDebugThrow_(debugAction_Alert);
  45.     SetDebugSignal_(debugAction_Alert);
  46.  
  47.         // Initialize Memory Manager. Parameter is the number of
  48.         // master pointer blocks to allocate
  49.     InitializeHeap(3);
  50.     
  51.         // Initialize standard Toolbox managers
  52.     UQDGlobals::InitializeToolbox(&qd);
  53.     
  54.         // Install a GrowZone to catch low-memory situations    
  55.     new LGrowZone(20000);
  56.  
  57.     new UMainThread;
  58.         // Create the application object and run
  59.     CBasicApp    theApp;
  60.     theApp.Run();
  61.     
  62.     return 0;
  63. }
  64.  
  65.  
  66. // ---------------------------------------------------------------------------
  67. //    • CBasicApp                                        [public]
  68. // ---------------------------------------------------------------------------
  69. //    Application object constructor
  70.  
  71. CBasicApp::CBasicApp():
  72.     moduleLibrary(0)
  73. {
  74.     RegisterClasses();
  75. }
  76.  
  77.  
  78. // ---------------------------------------------------------------------------
  79. //    • ~CBasicApp                                    [public, virtual]
  80. // ---------------------------------------------------------------------------
  81. //    Application object destructor
  82.  
  83. CBasicApp::~CBasicApp()
  84. {
  85.     // Nothing
  86. }
  87.  
  88.  
  89. // ---------------------------------------------------------------------------
  90. //    • StartUp                                        [protected, virtual]
  91. // ---------------------------------------------------------------------------
  92. //    Perform an action in response to the Open Application AppleEvent.
  93. //    Here, issue the New command to open a window.
  94.  
  95. void
  96. CBasicApp::StartUp()
  97. {
  98.     if ((long) OTAPRegisterAutoPushOnTCP ==
  99.                 kUnresolvedCFragSymbolAddress ) {
  100.         UModalAlerts::Alert( kAlert_NeedsAutoPush );
  101.         ObeyCommand( cmd_Quit, nil );
  102.     }
  103.     
  104.     ObeyCommand(cmd_New, nil);
  105. }
  106.  
  107. void    
  108. CBasicApp::Unload()
  109. {
  110.     OSStatus    err;
  111.     
  112.     if ( moduleLibrary ) {
  113.         err = OTAPUnregisterAutoPushOnUDP( kModuleName );
  114.         dprintf( "OTAPUnregisterAutoPushOnUDP error was %d\n", err );
  115.         err = OTAPUnregisterAutoPushOnTCP( kModuleName );
  116.         dprintf( "OTAPUnregisterAutoPushOnTCP error was %d\n", err );
  117.         OTReleaseCFMConnection( &moduleLibrary );
  118.         moduleLibrary = 0;
  119.         dprintf( "Unload\n" );
  120.     } else {
  121.         dprintf( "Not loaded\n" );
  122.     }
  123. }
  124.  
  125.  
  126. void    
  127. CBasicApp::Load()
  128. {
  129.     OSStatus    err;
  130.     
  131.     if ( !moduleLibrary ) {
  132.         err = OTLoadCFMLibrary("OTModl$MoreKittiesMod", &moduleLibrary, kOTLoadACopy);
  133.         dprintf( "OTLoadCFMLibrary error was %d\n", err );
  134.         err = OTAPRegisterAutoPushOnTCP( kModuleName );
  135.         dprintf( "OTAPRegisterAutoPushOnTCP error was %d\n", err );
  136.         err = OTAPRegisterAutoPushOnUDP( kModuleName );
  137.         dprintf( "OTAPRegisterAutoPushOnUDP error was %d\n", err );
  138.         dprintf( "Load\n" );
  139.     } else {
  140.         dprintf( "Already loaded\n" );
  141.     }
  142. }
  143.  
  144. UInt32
  145. CBasicApp::GetFlags()
  146. {
  147.     return (m3133T->GetValue() ? k3133t: 0) |
  148.             (mHeaders->GetValue() ? kHeaders: 0) |
  149.             (mHamster->GetValue() ? kHamsters: 0) |
  150.             (mKitties->GetValue() ? kKittiesFlag: 0);
  151. }
  152.  
  153.  
  154. void    
  155. CBasicApp::ListenToMessage( MessageT        inMessage,
  156.                             void*            )
  157. {
  158.     SetFilterFunc    routine;
  159.     switch (inMessage) {
  160.         case cmd_Set:
  161.             dprintf( "set called with %x\n", GetFlags() );        
  162.             if ( moduleLibrary ) {
  163.                 routine = (SetFilterFunc)OTGetCFMSymbol("SetFilter", moduleLibrary, kOTGetCodeSymbol);
  164.                 if ( routine ) {
  165.                     (*routine)( GetFlags() );
  166.                 } else {
  167.                     dprintf( "Could not find symbol for SetFilterFunc\n" );
  168.                 }
  169.             }
  170.             break;
  171.                         
  172.         case cmd_Unload:
  173.             Unload();            
  174.             break;
  175.             
  176.         case cmd_Load:
  177.             Load();
  178.             break;
  179.  
  180.         default: {
  181.             break;
  182.         }
  183.     }
  184.  
  185. }
  186.  
  187. // ---------------------------------------------------------------------------
  188. //    • ObeyCommand                                    [public, virtual]
  189. // ---------------------------------------------------------------------------
  190. //    Respond to Commands. Returns true if the Command was handled, false if not.
  191.  
  192. Boolean
  193. CBasicApp::ObeyCommand(
  194.     CommandT    inCommand,
  195.     void*        ioParam)
  196. {
  197.     Boolean        cmdHandled = true;    // Assume we'll handle the command
  198.  
  199.     switch (inCommand) {
  200.  
  201.         case cmd_New: {
  202.             LWindow* theWindow = LWindow::CreateWindow(PPob_SampleWindow, this);
  203.             ThrowIfNil_(theWindow);
  204.             UReanimator::LinkListenerToBroadcasters( this,
  205.                                                         theWindow,
  206.                                                         PPob_SampleWindow );
  207.             m3133T = (LStdCheckBox *)theWindow->FindPaneByID( id_3133T );
  208.             if ( !m3133T ) {
  209.                 dprintf( "m3133T was not found\n" );
  210.             }
  211.  
  212.             mHeaders = (LStdCheckBox *)theWindow->FindPaneByID( id_Headers );
  213.             if ( !m3133T ) {
  214.                 dprintf( "mHeaders was not found\n" );
  215.             }
  216.  
  217.             mKitties = (LStdCheckBox *)theWindow->FindPaneByID( id_Kitties );
  218.             if ( !m3133T ) {
  219.                 dprintf( "mKitties was not found\n" );
  220.             }
  221.  
  222.             mHamster = (LStdCheckBox *)theWindow->FindPaneByID( id_Hamster );
  223.             if ( !m3133T ) {
  224.                 dprintf( "mHamster was not found\n" );
  225.             }
  226.  
  227.             theWindow->Show();
  228.             break;
  229.         }
  230.  
  231.         case cmd_Quit:
  232.             Unload();
  233.             //fall through
  234.         default: {
  235.             cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
  236.             break;
  237.         }
  238.     }
  239.     
  240.     return cmdHandled;
  241. }
  242.  
  243.  
  244. // ---------------------------------------------------------------------------
  245. //    • FindCommandStatus                                [public, virtual]
  246. // ---------------------------------------------------------------------------
  247. //    Determine the status of a Command for the purposes of menu updating.
  248.  
  249. void
  250. CBasicApp::FindCommandStatus(
  251.     CommandT    inCommand,
  252.     Boolean&    outEnabled,
  253.     Boolean&    outUsesMark,
  254.     UInt16&        outMark,
  255.     Str255        outName)
  256. {
  257.     switch (inCommand) {
  258.  
  259.         case cmd_New: {
  260.             outEnabled = true;
  261.             break;
  262.         }
  263.             
  264.         default: {
  265.             LApplication::FindCommandStatus(inCommand, outEnabled,
  266.                                             outUsesMark, outMark, outName);
  267.             break;
  268.         }
  269.     }
  270. }
  271.  
  272.  
  273. // ---------------------------------------------------------------------------
  274. //    • RegisterClasses                                [protected]
  275. // ---------------------------------------------------------------------------
  276. //    To reduce clutter within the Application object's constructor, class
  277. //    registrations appear here in this seperate function for ease of use.
  278.  
  279. void
  280. CBasicApp::RegisterClasses()
  281. {
  282.     RegisterClass_(LWindow);
  283.     RegisterClass_(LCaption);
  284.     RegisterClass_(LStdButton);
  285.     RegisterClass_(LStdCheckBox);
  286.     RegisterClass_(LEditField);
  287. }